package com.example.dbwork;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.model.Shop;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBConnector {
	
	// Database data: tables, names, columns
	private static final String DATABASE_NAME = "comppricedb.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "Shops";

	// Table Shops, names of its columns
	private static final String ID = "_id";
	private static final String NAME = "name";
	private static final String ADDRESS = "address";

	// Column's numbers
	private static final int NUM_ID = 0;
	private static final int NUM_NAME = 1;
	private static final int NUM_ADDRESS = 2;
	
	private SQLiteDatabase compPriceDB;
	
	public DBConnector(Context context) {
		// Create/open database
		OpenHelper mOpenHelper = new OpenHelper(context);
		compPriceDB = mOpenHelper.getWritableDatabase();
	}	
	
	// Create DB Class
	private class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);	}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			String query = "CREATE TABLE " + TABLE_NAME + " (" + 
					ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
					NAME + " TEXT, " + 
					ADDRESS + " TEXT); ";
			db.execSQL(query);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}

	//Insert field into table Shop
	public long insertToShop(Shop shop) {
		ContentValues cv=new ContentValues();
		cv.put(NAME, shop.getShopName());
		cv.put(ADDRESS, shop.getShopAddress());
		return compPriceDB.insert(TABLE_NAME, null, cv);
	}

	//Update field in table Shop
	public int updateShop(Shop shop) {
		ContentValues cv=new ContentValues();
		cv.put(NAME, shop.getShopName());
		cv.put(ADDRESS, shop.getShopAddress());
		return compPriceDB.update(TABLE_NAME, cv, ID + " = ?", new String[] {String.valueOf(shop.get_id()) });
	}
	
	//Delete field in table Shop
	public void deleteShop(long id) {
		compPriceDB.delete(TABLE_NAME, ID + " = ?", new String[] { String.valueOf(id) });
	}

	//Select one field method
	public Shop selectShop(long id) {
		Cursor mCursor = compPriceDB.query(TABLE_NAME, null, ID + " = ?", new String[] {String.valueOf(id)}, null, null, NAME);
		
		mCursor.moveToFirst();
		String nameShop = mCursor.getString(NUM_NAME);
		String addressShop = mCursor.getString(NUM_ADDRESS);
		mCursor.close();
		return new Shop(id, nameShop, addressShop);
	}
	
	//Select all fields method
	public ArrayList<Shop> selectAllShops() {
		Cursor mCursor = compPriceDB.query(TABLE_NAME, null, null, null, null, null, NAME);
		
		ArrayList<Shop> arr = new ArrayList<Shop>();
		mCursor.moveToFirst();
		if (!mCursor.isAfterLast()) {
			do {
				long id = mCursor.getLong(NUM_ID);
				String nameShop = mCursor.getString(NUM_NAME);
				String addressShop = mCursor.getString(NUM_ADDRESS);
				arr.add(new Shop(id, nameShop, addressShop));
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		return arr;
	}
	
	//Select all fields method return hashMap array
	public ArrayList<HashMap<String, String>> selectAllShops(String hash) {
		Cursor mCursor = compPriceDB.query(TABLE_NAME, null, null, null, null, null, NAME);
		
		ArrayList<HashMap<String, String>> arr = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;

		mCursor.moveToFirst();
		if (!mCursor.isAfterLast()) {
			do {
				map = new HashMap<String, String>();
				map.put(NAME, mCursor.getString(NUM_NAME));
				map.put(ADDRESS, mCursor.getString(NUM_ADDRESS));
				arr.add(map);
			} while (mCursor.moveToNext());
		}
		mCursor.close();
		return arr;
	}
}
